/*
 * 
 * Created on Jul 11, 2005
 */
package com.wgo.precise.client.ui.model;

import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.wgo.precise.client.ui.controller.RequirementPlugin;


/**
 * This class is protected to controll dirty and notifyModelChange..

 * @author Petter L. H. Eide
 * @version $Id: PropertyValue.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public abstract class PropertyValue {

//    protected PropertyValue(Object value, AbstractPropertySpesification spesification) {
//        if (spesification == null) {
//            throw new RematoClientException("Error when creating new value. Spesification not set.");
//        } 
//        propertySpesification = spesification;
//        if (value != null) {
//            try {
//                this.value = spesification.getPropertyType().getClazz().cast(value);
//            } catch (ClassCastException e) {
//                throw new RematoClientException(new Status(IStatus.ERROR, Registry.PLUGIN_ID, 0, "Class-type-mismatch: " + value.getClass() + "(value) and " + spesification.getPropertyType().getClazz()+ "(PropertyType)", null));
//            }
//        } else {
//            setDefaultValue();
//        }
//    }

    public abstract AbstractPropertySpesification getPropertySpesification();

    public PropertyDescriptor getDescriptor() {
        return getPropertySpesification().getDescriptor();
    }
    
    public abstract Object getValue() ;
    
    protected abstract void setValue(Object value) ;
    

    /**
     * Needed for cell-editor. When using combo, shold implement own method in PropertyType
     */
	public Object getCellEditorValue() {
    	return getPropertySpesification().getPropertyType().createCellEditorValueFromObject(getValue());
    }

    /**
     * Needed for cell-editor. When using combo, shold implement own method in PropertyType
     */
    protected boolean setValueFromCellEditor(Object cellValue) {
        Object objectValue = getPropertySpesification().getPropertyType().createObjectValueFromCellEditor(cellValue);
        if (null == this.getValue() || ! this.getValue().equals(objectValue)) {
            setValue(objectValue);
            return true;
        }
        return false;
    }

//    protected abstract void setPropertySpesification(AbstractPropertySpesification propertySepesification) ;

    protected void setDefaultValue() {
        String defaultValue = getPropertySpesification().getDefaultValue();
        if (defaultValue != null ) {
            setValue(getPropertySpesification().getPropertyType().createObjectValue(defaultValue));
        } else {
        	setValue(getPropertySpesification().getPropertyType().createNewValue());
            RequirementPlugin.log(getClass() , "PropertyType.value has been created.");
        }
    }

  //    /**
//     * @see java.lang.Object#clone()
//     */
//    @Override
//    public PropertyValue clone() {
//        return new PropertyValue(this.value, this.propertySpesification);
//    }
   
}
/**
 * 
 * Changelog:
 * 
 * $Log: PropertyValue.java,v $
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.9  2006-01-13 08:12:42  petterei
 * Using Long consequently to differentiate between �null� and �0� for dbId and conceptId (no boxing/unboxing)
 * Ajusted code to stronger java-syntax
 *
 * Revision 1.8  2005-12-07 07:25:35  petterei
 * gui & logic for relationships
 *
 * Revision 1.7  2005-12-05 04:16:40  petterei
 * *** empty log message ***
 *
 * Revision 1.6  2005-11-28 21:45:10  petterei
 * refactored most of the client model to wrap the domain-model
 * added some functionality for new domain-model-concepts
 *
 * Revision 1.5  2005/10/24 16:25:36  petterei
 * *** empty log message ***
 *
 * Revision 1.4  2005/10/02 21:31:16  petterei
 * client with new domain model
 *
 * Revision 1.3  2005/09/30 15:33:23  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005/09/29 16:41:42  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/09/27 17:04:29  petterei
 * inittial commit
 *
 * Revision 1.5  2005/08/25 09:46:30  peide3
 * added batch update + bugfixes
 *
 * Revision 1.4  2005/08/23 06:41:14  peide3
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:09  petterei
 * initial commit
 *
 */
